﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.OpenAPI.Rss;
using DNA.Utility;

namespace DNA.Mvc.Services
{
    public class RssDocumentGenerator : CommandContainer
    {
        RssDocument rssdoc;

        public RssDocumentGenerator(IEnumerable<RssItemsGeneratorBase> rssItemGenerators) : this() { this.InnerItems.AddRange(rssItemGenerators); }
        public RssDocumentGenerator()
        {
            Reset();
        }

        public RssDocumentGenerator Reset()
        {
            rssdoc = new RssDocument() { Channel = new RssChannel() };
            rssdoc.Channel.Items = new List<RssItem>();
            return this;
        }

        public RssDocumentGenerator(string manifest) : this() { }

        public RssDocumentGenerator SetTitle(string title)
        {
            rssdoc.Channel.Title = title;
            return this;
        }

        public RssDocumentGenerator SetDesc(string desc)
        {
            rssdoc.Channel.Description = desc;
            return this;
        }

        public RssDocumentGenerator SetCopyright(string copyright)
        {
            rssdoc.Channel.Copyright = copyright;
            return this;
        }

        public RssDocumentGenerator SetLanguage(string language)
        {
            rssdoc.Channel.Language = language;
            return this;
        }

        public RssDocumentGenerator SetLink(string link)
        {
            this.rssdoc.Channel.Link = link;
            return this;
        }

        public RssDocumentGenerator SetWebmasterEmail(string email)
        {
            this.rssdoc.Channel.WebMaster = email;
            return this;
        }

        public RssDocumentGenerator SetCategories(string[] categories)
        {
            this.rssdoc.Channel.Categories = categories.ToList();
            return this;
        }

        public RssDocumentGenerator SetTTL(int ttl)
        {
            rssdoc.Channel.TTL = ttl;
            return this;
        }

        public RssDocumentGenerator SetImage(string imageUrl, string title = "", string desc = "", string navigateUrl = "", int width = 0, int height = 0)
        {
            rssdoc.Channel.Image = new RssImage() { ImageUrl = imageUrl };
            if (string.IsNullOrEmpty(title)) rssdoc.Channel.Image.Title = title;
            if (string.IsNullOrEmpty(desc)) rssdoc.Channel.Image.Description = desc;
            if (string.IsNullOrEmpty(navigateUrl)) rssdoc.Channel.Image.NavigateUrl = navigateUrl;
            if (width > 0) rssdoc.Channel.Image.Width = width;
            if (height > 0) rssdoc.Channel.Image.Height = height;
            return this;
        }

        public RssDocumentGenerator SetDocUrl(string docUrl)
        {
            this.rssdoc.Channel.DocUrl = docUrl;
            return this;
        }

        public RssDocumentGenerator SetPubDate(DateTime date)
        {
            rssdoc.Channel.PubDate = date.ToString("r");
            return this;
        }

        public RssDocumentGenerator SetLastBuildDate(DateTime date)
        {
            rssdoc.Channel.LastBuildDate = date.ToString("r");
            return this;
        }

        public RssDocumentGenerator SetManagingEditor(string editor)
        {
            rssdoc.Channel.ManagingEditor = editor;
            return this;
        }

        public RssDocumentGenerator SetGenerator(string generator)
        {
            rssdoc.Channel.Generator = generator;
            return this;
        }

        public RssDocument GetRssDoc(object _params=null)
        {
            Invoke(new CommandContext(_params) { Bag = rssdoc });
            return rssdoc;
        }

        public string GetXml(object _params = null)
        {
            string xml = XmlSerializerUtility.SerializeToXml(this.GetRssDoc(_params));
            return xml;
        }
    }
}
